#ifdef ENABLE_NETWORK

#include "../menu.h"
#include "user.h"
#include "Win15PicEvent_net.h"
#include "logo_dat.h" 
#include "..\aw_PIC_app.h"
#include "..\aw_gui_app.h"
#include "aw_pic_util.h"
#include "Fs_playlist.h"
#include <aw_playlist2.h>
#include"NetmenuSetupEvent.h"
extern void PicParaTrans(void);
extern int pic_slide_show_mode;
extern int MultiMenuToSingle;
extern int SHOW_WINDOW_NUM;
INT32U preview_files_number=0;
INT32U preview_files_pages=0;
INT32U preview_current_page=1;
INT8U  preview_focus_id=1;
static unsigned char preview_select_dir=0;
static unsigned char preview_select_dir_level=0;
static INT8U preview_InitFileList_flag=0;
static INT8U current_frame_number=0;
static AVTimer_t win_switch_timer = 0;
static INT8U keydown_flag=0;
static INT32U bk_key=UOP_NOP;
static INT8U prepare_get_num;
static int prepare_ret;
char gpFileList_net[15][STORE_FILE_NAME_LENGTH] ;
PreviewFileListItem file_item[15];
PreviewFileListItem* gCurFileItem = NULL;
char gCurDirName_net[STORE_FILE_NAME_LENGTH] ;
INT8U enable_playstatus = 0;
extern int pic_play_status;

extern int PreviewPicPlayer(INT8U max_preview_num);
extern void RefreshCurFileName(void);
extern char SixPicNumInfo[20];

void InitPreviewList(void)
{
	preview_InitFileList_flag=1;
}

void Play_File(int play_status)
{
	INT16U item_index;
	INT32U item_type,app_type;
	int ret = -1;
	unsigned media_type=MEDIA_PIC &NAV_PATH_TYPE;
	item_type=gCurFileItem->path_type;
	item_index=gCurFileItem->filelist_item_id;
	if(item_type & MEDIA_PIC){
		 Playlist_t *playlist=NULL;
		 playlist = fs_playlist_create(NULL,media_type,
					PLAYLIST_TYPE_IMAGE, item_index,0, &app_type);
		 if(playlist){
			enable_playstatus = 1;
			pic_play_status = play_status;
			PicParaTrans();  
			ret = RunPICPlayer(playlist,(unsigned char *)Logo_keyboard, sizeof(Logo_keyboard), 0);
			if(ret){
				AWPlaylistDelete2(playlist);
				enable_playstatus = 0;
			}else{
				ReloadLogoType(0);
				GUIHideWindow("Win15Pic_net");
#ifdef ENABLE_CURSOR
                HideCursor();
                ShowCursor(400,200);
#endif				
			}
                }
	}
}


INT32U GetDirFileNum(char* dir_name,INT32U *file_num,INT32U *dir_num,INT32U gGetFileType)
{
	INT32U count=0;
	INT32U media_type=gGetFileType&NAV_PATH_TYPE;
    char* dev_str;
    if(dir_name)
		dev_str = FileNavGetDeviceTypeStr(dir_name);
    else
		dev_str = FileNavGetDeviceTypeStr(FileNavGetCurrDir());

	FileNavGetCount(dir_name,media_type,file_num,dir_num,0);

	if(Is_Gallery(dev_str)){    
  	    if((*dir_num)>0)
  	        *file_num=0;
  	}
  	return count;
}

void re_calculate_preview_files_num(unsigned init)
{
    /* GetDirFileNum() can only gives the rough count (total count of dir and files);
        below code gives the accurate file count that match the filter */
    INT32U get_list_num;
    INT32U temp;
    PATHNODE file_list ;
    int i, file_count;
    int pos;
    file_count=0;
    if(init){
	GetDirFileNum(NULL,&preview_files_number,&temp,MEDIA_PIC& NAV_PATH_TYPE);	
	preview_files_pages =0;
	preview_current_page =1;
	preview_focus_id=1;
	if(preview_files_number)
	 	preview_files_pages=(preview_files_number+(SHOW_WINDOW_NUM-1))/SHOW_WINDOW_NUM;
        pos=0;
    }
    else{    
        if(file_item[SHOW_WINDOW_NUM-1].filelist_item_id==0xffff)
	    return;
	pos=file_item[SHOW_WINDOW_NUM-1].filelist_item_id+1;    
    }
    FileNavSeek(pos);
	    
    for(i=0;i<SHOW_WINDOW_NUM;i++){	    
       get_list_num=1;
	FileNavGetNextFileList(&file_list, &get_list_num, MEDIA_PIC& NAV_PATH_TYPE) ;
	if(get_list_num!=0)
	    file_count++;
        else
            break;
    }  
    if(i<SHOW_WINDOW_NUM){
        if(init){
            preview_files_number=file_count;
            preview_files_pages=1; 
            current_frame_number=file_count; 
        }
        else{    
            preview_files_number=preview_current_page*SHOW_WINDOW_NUM+file_count;
            if(file_count>0){
                preview_files_pages=preview_current_page+1; 
                current_frame_number=file_count; 
            }
            else{
                preview_files_pages=preview_current_page; 
                current_frame_number=SHOW_WINDOW_NUM; 
            }            
        }            
    }        
}    

char *GetFileItemName(INT8U File_offset,INT8U max_num)
{
	INT8U offset =File_offset-1;
	if(offset<max_num)
		return (char *)file_item[offset].file_name;
	return NULL;
}

PreviewFileListItem *GetFileItemInfo(INT8U File_offset,INT8U max_num)
{
	INT8U offset =File_offset-1;
	if((offset<max_num)&&(file_item[offset].filelist_item_id!=0xffff))
		return &file_item[offset];
	return NULL;
}

INT32U GetFileItemType(INT8U File_offset,INT8U max_num)
{
	INT8U offset =File_offset-1;
	if(offset<max_num)
		return file_item[offset].path_type;
	return 0;
}

INT16U GetFileItemIndex(INT8U File_offset,INT8U max_num)
{
	INT8U offset =File_offset-1;
	if(offset<max_num)
		return file_item[offset].filelist_item_id;
	return 0;
}

void SetFilelist(INT8U list_num, PreviewFileListItem *entry_lists)
{
	for(int i=0; i < list_num; i++){
        entry_lists[i].file_name = (INT32U)gpFileList_net[i] ;
       }
}

INT32U GetPreviewIcon(INT32U file_type,INT8U preview_flag,INT8U focus_flag)
{
	if(file_type & MEDIA_PIC){
		if(preview_flag){
			if(focus_flag)
				return (INT32U)ICON_PIC_FOCUS;
			else
				return (INT32U)ICON_PIC_LOSE;
		}else{
			if(focus_flag)
				return (INT32U)ICON_PIC_FOCUS;
			else
				return (INT32U)ICON_PIC_LOSE;
		}

	}else if(file_type & NAV_IS_DIR){
		if(focus_flag)
			return (INT32U)ICON_FOLDER_FOCUS;
		else
			return (INT32U)ICON_FOLDER_LOSE;
	}else{
			return (INT32U)ICON_PIC_DISABLE;
	}
}

INT8U IsFocusWin(INT8U item_index)
{
	PHWND dir_hwnd=NULL;
	INT8U max_num=0;
	max_num=SHOW_WINDOW_NUM;
	dir_hwnd=AWFindWinCtrlByName("15PicBar_net");
	
	if((!dir_hwnd)||(file_item[item_index-1].filelist_item_id==0xffff)||(item_index>max_num))
		return 0;
	if((dir_hwnd->draw_info[item_index+1].draw_data==(INT32U)ICON_PIC_FOCUS)||(dir_hwnd->draw_info[item_index+1].draw_data==(INT32U)ICON_FOLDER_FOCUS))
		return 1;
	return 0;
}

void UpdatePreviewShow(INT8U item_index,INT8U preview_flag)
{
	PHWND dir_hwnd=NULL;
	dir_hwnd=AWFindWinCtrlByName("15PicBar_net");
	if((!dir_hwnd)||(file_item[item_index-1].filelist_item_id==0xffff)||(item_index>SHOW_WINDOW_NUM))
		return;
	file_item[item_index-1].preview_flag=preview_flag;
	if(dir_hwnd->draw_info[item_index+1].draw_data!=(INT32U)GetPreviewIcon(file_item[item_index-1].path_type,file_item[item_index-1].preview_flag,IsFocusWin(item_index))){
        if(item_index!=0)
		dir_hwnd->draw_info[item_index+1].draw_data = (INT32U)GetPreviewIcon(file_item[item_index-1].path_type,file_item[item_index-1].preview_flag,IsFocusWin(item_index));
		if(AWIsVisibleWinControl(dir_hwnd)){
			AWPostMessage(dir_hwnd, WM_PAINT_DRAW_INFO, item_index+1);
			AWPostMessage(dir_hwnd, WM_PAINT_FINISH, 0);			
		}
	}
}

void FlashPreviewFrame(PHWND hwnd,INT8U HL_flag,INT8U max_num)
{
	INT8U i;
	for(i=0;i<max_num;i++){
		if((gpFileList_net[i][0]==0)||(file_item[i].filelist_item_id==0xffff)){
			hwnd->draw_info[i+2].draw_data = (INT32U)ICON_PIC_DISABLE;
		}
		else if(file_item[i].path_type & MEDIA_PIC){
			if(file_item[i].preview_flag){
				if(i==(HL_flag-1))
					if(file_item[i].selected_flag)
						hwnd->draw_info[i+2].draw_data = (INT32U)ICON_PIC_FOCUS;
					else
						hwnd->draw_info[i+2].draw_data = (INT32U)ICON_PIC_FOCUS;
				else
					if(file_item[i].selected_flag)
						hwnd->draw_info[i+2].draw_data = (INT32U)ICON_PIC_LOSE;
					else
						hwnd->draw_info[i+2].draw_data = (INT32U)ICON_PIC_LOSE;
			}else{
				if(i==(HL_flag-1))
					if(file_item[i].selected_flag)
						hwnd->draw_info[i+2].draw_data = (INT32U)ICON_PIC_FOCUS;
					else
						hwnd->draw_info[i+2].draw_data = (INT32U)ICON_PIC_FOCUS;
				else
					if(file_item[i].selected_flag)
						hwnd->draw_info[i+2].draw_data = (INT32U)ICON_PIC_LOSE;
					else
						hwnd->draw_info[i+2].draw_data = (INT32U)ICON_PIC_LOSE;
			}
		}else if(file_item[i].path_type & NAV_IS_DIR){
			if(i==(HL_flag-1))
				hwnd->draw_info[i+2].draw_data = (INT32U)ICON_FOLDER_FOCUS;
			else
				hwnd->draw_info[i+2].draw_data = (INT32U)ICON_FOLDER_LOSE;			
		}else{
			hwnd->draw_info[i+2].draw_data = (INT32U)ICON_PIC_DISABLE;
		}
	}
}

void ClearFileItem(PreviewFileListItem *entry_lists,INT8U max_num)
{
	INT8U i;
	for(i=0;i<max_num;i++){
		memset((char *)entry_lists[i].file_name,0,STORE_FILE_NAME_LENGTH);
		entry_lists[i].filelist_item_id=0xffff;
		entry_lists[i].path_type=0;
		entry_lists[i].preview_flag=0;
		entry_lists[i].selected_flag=0;
	}
}

void ClearPreviewflag(void)
{
	for(INT8U i=0;i<SHOW_WINDOW_NUM;i++){
		file_item[i].preview_flag=0;
	}
}

void FileListSetName_net(char *listname, char *filename)
{
    strncpy(listname, filename, STORE_FILE_NAME_LENGTH-1) ;
    listname[STORE_FILE_NAME_LENGTH-1] = 0 ;
}

INT8U IsPreviewEnable(INT8U File_offset,INT8U max_num)
{
	INT8U offset =File_offset-1;
	if(offset<max_num)
		return file_item[offset].preview_flag;
	return 0;
}

INT32S GetFileListCB_net(INT32S start_pos, INT8U *get_num, PreviewFileListItem *entry_lists, INT8U next_flag, INT32U gGetFileType)
{
    INT32S have_more_file = 1 ;
    INT32U get_list_num = 1;
    INT8U list_index = 0 ;
    INT8U limit_num=*get_num;
    PATHNODE file_list;
    char *file_name_addr ;
#ifdef ENABLE_CURSOR
    ShowCursor(400,200);
#endif	
    FileNavSeek(start_pos) ;
    ClearFileItem(entry_lists,limit_num);
    while((list_index < limit_num)&&(have_more_file)){
        if(next_flag)
            have_more_file = FileNavGetNextFileList(&file_list, &get_list_num, gGetFileType ) ;
        else
            have_more_file = FileNavGetPrevFileList(&file_list, &get_list_num, gGetFileType ) ;
        if(get_list_num){
                if((file_list.path_type & gGetFileType) || (file_list.path_type & NAV_IS_DIR))
                {
			if(next_flag){
				entry_lists[list_index].filelist_item_id = file_list.index ; 
	            		entry_lists[list_index].path_type = file_list.path_type ; 
	            		file_name_addr = (char *)entry_lists[list_index].file_name;
	           		FileListSetName_net(file_name_addr, file_list.short_name) ;
				entry_lists[list_index].preview_flag=0;
				entry_lists[list_index].selected_flag=0;
			}else{
				entry_lists[limit_num-list_index-1].filelist_item_id = file_list.index ; 
	            		entry_lists[limit_num-list_index-1].path_type = file_list.path_type ; 
	            		file_name_addr = (char *)entry_lists[limit_num-list_index-1].file_name;
	           		FileListSetName_net(file_name_addr, file_list.short_name) ;
				entry_lists[limit_num-list_index-1].preview_flag=0;
				entry_lists[limit_num-list_index-1].selected_flag=0;
			}
           		list_index++ ;
                }else{
			continue;
            	  }
	  }else{
		break;
	  }
    }
    get_list_num=1;
    while((have_more_file)&&(get_list_num)){ /* Though "have more file", we need check if there is more file that has gGetFileType */
        if(next_flag)
            FileNavGetNextFileList(&file_list, &get_list_num, gGetFileType ) ;
        else
            FileNavGetPrevFileList(&file_list, &get_list_num, gGetFileType ) ;
        if(get_list_num==0){
            have_more_file=0;
        }else if((file_list.path_type & gGetFileType) || (file_list.path_type & NAV_IS_DIR)){
	     break;
        }
    }   
    *get_num = list_index;
#ifdef ENABLE_CURSOR
    HideCursor();
#endif	
	
    return have_more_file ;
}

INT8U PageUp_Down(PHWND hwnd,INT8U direction,INT32U gGetFileType,INT8U max_num)  //0 for up 1 for down
{
	INT8U get_num=max_num;
	INT32S start_pos=0;
	if(direction){
		if(file_item[max_num-1].filelist_item_id==0xffff)
			return 0;
		start_pos= file_item[max_num-1].filelist_item_id+1;
		GetFileListCB_net(start_pos, &get_num, file_item, 1,gGetFileType);
		if(get_num){
			FlashPreviewFrame(hwnd,1,max_num);
			PreviewPicPlayer(get_num);
			return 1;
		}else
			return 0;		
	}else{
		if(file_item[0].filelist_item_id==0xffff)
			return 0;
		start_pos= file_item[0].filelist_item_id-1;
		GetFileListCB_net(start_pos, &get_num, file_item, 0,gGetFileType);
		if(get_num<max_num){
			if(file_item[0].filelist_item_id==0xffff)
				return 0;
			start_pos= file_item[0].filelist_item_id;
			GetFileListCB_net(start_pos, &get_num, file_item, 1,gGetFileType);
		}
		if(get_num){
			FlashPreviewFrame(hwnd,max_num,max_num);
			PreviewPicPlayer(max_num);
		}
		return get_num;
	}
}

INT32S CDNewDir_net(INT32S entry_pos)
{
    INT32S have_more_file = 1 ;
    INT32U get_list_num = 1;
    PATHNODE file_list ;
    INT32U gGetFileType =NAV_IS_DIR;
    INT32S ret = -1;
    char *temp;
    if(entry_pos == -1){ //back up dir
        ret = FileNavCD("..") ;
    }
    else{
        FileNavSeek(entry_pos) ;
        have_more_file = FileNavGetNextFileList(&file_list, &get_list_num, gGetFileType ) ;
        if((get_list_num)&&(file_list.path_type & NAV_IS_DIR)){
            ret = FileNavCD(file_list.short_name) ;
        }
    }
    return ret ;
}


int ProcessCDNewDir(PHWND hwnd,INT8U pos,INT32U *folder_num,INT8U direction,INT32U gGetFileType)		//0 for cd.. 1 for cd folder name
{
	int ret,folder_ret;
	char *temp=NULL;
	INT32U file_num=0;
	INT8U get_num=SHOW_WINDOW_NUM;
	if(direction){
		ret=CDNewDir_net(pos);
		if(ret>=0)
			GetDirFileNum(NULL,&file_num,folder_num,gGetFileType);
		else 
			return -1;
		ret=GetFileListCB_net(0,&get_num,file_item,1,gGetFileType);
		FlashPreviewFrame(hwnd,1,SHOW_WINDOW_NUM);
		PreviewPicPlayer(get_num);
		ret=file_num;
	}else{
		ret=CDNewDir_net(-1);
		if(ret>=0)
			GetDirFileNum(NULL,&file_num,folder_num,gGetFileType);
		else
			return -1;
		ret=GetFileListCB_net(0,&get_num,file_item,1,gGetFileType);
		FlashPreviewFrame(hwnd,1,SHOW_WINDOW_NUM);
		PreviewPicPlayer(get_num);
		ret=file_num;
	}	
	temp=FileNavGetCurrDir();
	memset(gCurDirName_net,0,STORE_FILE_NAME_LENGTH);
    	if(strlen(temp)<=(STORE_FILE_NAME_LENGTH-1)){
		strcat(gCurDirName_net,temp);
    	}
	return ret;
}

void Init_FileList_Prepare_List(INT32U gGetFileType,INT8U max_num)
{
	prepare_get_num=max_num;
	char *path_name=FileNavGetCurrDir();
	SetFilelist(SHOW_WINDOW_NUM,file_item);
	if(path_name[0]==0){
	 	prepare_get_num=0;
	 	return;
	} 	
    	memset(gCurDirName_net,0,STORE_FILE_NAME_LENGTH);
    	if(strlen(path_name)<=(STORE_FILE_NAME_LENGTH-1)){
		strcat(gCurDirName_net,path_name);
    	}
	prepare_ret=GetFileListCB_net(0,&prepare_get_num,file_item,1,gGetFileType);
	return;
    
}

INT8U Init_FileList(PHWND hwnd, INT8U max_num)
{
	FlashPreviewFrame(hwnd,1,max_num);
	PreviewPicPlayer(prepare_get_num);
	return 0;
}

void RefreshCurFileName(void)
{
    INT32S TotalPages = preview_files_pages;
    INT32S CurrentPage = preview_current_page;
    PreviewFileListItem *cur_file_item=NULL;
	PHWND hwnd = AWFindWinCtrlByName("15PicBar_net") ;
	if(!hwnd)
		return;
	if(CurrentPage>TotalPages)
		TotalPages = CurrentPage;
    sprintf(SixPicNumInfo,"%d/%d", CurrentPage,TotalPages) ;
    //static char PicHeightWidth[20];
    //PicHeightWidth[0]='0';
    //sprintf(PicHeightWidth,"%dX%d",infos[aw_title_num%SHOW_WINDOW_NUM].picinfo.height,infos[aw_title_num%SHOW_WINDOW_NUM].picinfo.width);
    hwnd->draw_info[SHOW_WINDOW_NUM+3].draw_data = (INT32U) SixPicNumInfo;
    cur_file_item = GetFileItemInfo(preview_focus_id, SHOW_WINDOW_NUM);
    if((cur_file_item) && (cur_file_item->filelist_item_id != 0xffff))
    	hwnd->draw_info[SHOW_WINDOW_NUM+5].draw_data = (INT32U)((char *)cur_file_item->file_name);
    else
	hwnd->draw_info[SHOW_WINDOW_NUM+5].draw_data = (INT32U)0;	
    //hwnd->draw_info[SHOW_WINDOW_NUM+6].draw_data = (INT32U) PicHeightWidth;
    //if((infos[aw_title_num%SHOW_WINDOW_NUM].picinfo.time_info[0])==0)
    //hwnd->draw_info[SHOW_WINDOW_NUM+6].draw_data = (INT32U)picshowtime ;	
    //else
    //hwnd->draw_info[SHOW_WINDOW_NUM+6].draw_data = (INT32U) infos[aw_title_num%SHOW_WINDOW_NUM].picinfo.time_info;  
    hwnd->draw_info[SHOW_WINDOW_NUM+2].draw_data = (INT32U)ICON_PAGE_BACKROUNG;
    hwnd->draw_info[SHOW_WINDOW_NUM+4].draw_data = (INT32U)ICON_PHOTO_INFO_BACKROUNG;
    if(AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName("Win15Pic_net")) {
    	AWPostMessage(hwnd,WM_PAINT_DRAW_INFO,SHOW_WINDOW_NUM+2);
    	AWPostMessage(hwnd,WM_PAINT_DRAW_INFO,SHOW_WINDOW_NUM+3);
    	AWPostMessage(hwnd,WM_PAINT_DRAW_INFO,SHOW_WINDOW_NUM+4);
    	AWPostMessage(hwnd,WM_PAINT_DRAW_INFO,SHOW_WINDOW_NUM+5);
    	AWPostMessage(hwnd,WM_PAINT_DRAW_INFO,SHOW_WINDOW_NUM+6);
    	AWPostMessage(hwnd,WM_PAINT_FINISH,0);
    }
}


void Win15PicBar_netOnKeyDo(INT32U key)
{
    INT8U preview_focus_Pic;
	PreviewFileListItem *cur_file_item=NULL;
	PHWND hwnd=AWFindWinCtrlByName("15PicBar_net");
	
	if(preview_current_page<preview_files_pages)
		current_frame_number=SHOW_WINDOW_NUM;
	else
		current_frame_number=preview_files_number%SHOW_WINDOW_NUM;
	if(!current_frame_number)
		current_frame_number=SHOW_WINDOW_NUM;
	if(!preview_files_number){
		current_frame_number=0;
		preview_focus_id=1;
	}
	ReloadLogoType(0);
	switch (key){
		case UOP_RESUME:
		case UOP_OSD:
		case UOP_STOP:
	        if(CheckMediaPlaybackApp())
		        CloseMediaPlaybackApp(0xff);		
			if(preview_select_dir_level==0){
				FileNavCD("..");
				Preview15net_quit();
				GUIShowWindow("WinDeviceSelect");
		   	}else{
		   		int temp_value;
				INT32U folder_num = 0;
		   		temp_value = ProcessCDNewDir(hwnd,0, &folder_num, 0, MEDIA_PIC & NAV_PATH_TYPE);
		   		if(temp_value>=0){
					preview_files_number = temp_value + folder_num;
					char *dev_str = FileNavGetDeviceTypeStr(FileNavGetCurrDir());
					if(dev_str){
						//if(Is_Gallery(dev_str))  {
              					//re_calculate_preview_files_num(0); /* recalculate preview_files_pages,etc */
						//}
					}
					preview_files_pages=0;
					preview_focus_id = 0;
					if(preview_files_number){
					 	preview_files_pages = (preview_files_number+(SHOW_WINDOW_NUM-1))/SHOW_WINDOW_NUM;
						preview_focus_id = 1;
					}
					preview_select_dir_level--;
					preview_current_page=1;
					for(INT8U i=0;i<SHOW_WINDOW_NUM;i++)
						AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, i+2) ;
					AWPostMessage(hwnd, WM_PAINT_FINISH, 0) ;
					RefreshCurFileName();
				}
			}
	       	break;
	     	case UOP_ENTER:	
		case UOP_END_PAUSE:
			AWSetFileList(gpFocusWin,NULL,NULL);  
			gCurFileItem=GetFileItemInfo(preview_focus_id, SHOW_WINDOW_NUM);
			if(!gCurFileItem)
				break;
			if(gCurFileItem->filelist_item_id == 0xffff)
				break;
			if(gCurFileItem->path_type & NAV_IS_DIR)
				preview_select_dir = 1;
			else
				preview_select_dir = 0;
		    	if(preview_select_dir){
		   		int temp_value;
				INT32U folder_num = 0;
		   		temp_value = ProcessCDNewDir(hwnd,gCurFileItem->filelist_item_id, &folder_num, 1, MEDIA_PIC & NAV_PATH_TYPE);
		   		if(temp_value>=0){
					preview_files_number = temp_value + folder_num;
					char *dev_str = FileNavGetDeviceTypeStr(FileNavGetCurrDir());
					if(dev_str){
						//if(Is_Gallery(dev_str))  {
              					//re_calculate_preview_files_num(0); /* recalculate preview_files_pages,etc */
						//}
					}
					preview_files_pages=0;
					preview_focus_id = 0;
					if(preview_files_number){
					 	preview_files_pages = (preview_files_number+(SHOW_WINDOW_NUM-1))/SHOW_WINDOW_NUM;
						preview_focus_id = 1;
					}
					preview_select_dir_level++;
					preview_current_page=1;
					for(INT8U i=0;i<SHOW_WINDOW_NUM;i++)
						AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, i+2) ;
					AWPostMessage(hwnd, WM_PAINT_FINISH, 0) ;
					RefreshCurFileName();
				}      
                     }else{				
				if(gCurFileItem){
                    MultiMenuToSingle=1;
					Play_File((key==UOP_ENTER)?1:0);
				}	
                     }
	       	break;	
	    	case UOP_PREVIOUS_CHAP:	    	
    	    	case UOP_FAST_BACKWARD:
		case UOP_PAGE_UP:
			preview_focus_Pic=preview_focus_id;
			preview_focus_id=PageUp_Down(hwnd, 0, MEDIA_PIC & NAV_PATH_TYPE, SHOW_WINDOW_NUM);
			if(preview_focus_id){
				char *dev_str = FileNavGetDeviceTypeStr(FileNavGetCurrDir());
				if(dev_str){
					//if(Is_Gallery(dev_str))  {
              				//re_calculate_preview_files_num(0); /* recalculate preview_files_pages,etc */
					//}
				}
				for(INT8U i=0;i<SHOW_WINDOW_NUM;i++)
					AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, i+2) ;
				AWPostMessage(hwnd, WM_PAINT_FINISH, 0) ;
				preview_current_page--;
				RefreshCurFileName();
			}				
			else   
				{
				  if(preview_focus_Pic!=0)
				  	{
					 if(file_item[preview_focus_Pic-1].path_type & MEDIA_PIC)
					 	 hwnd->draw_info[preview_focus_Pic+1].draw_data = (INT32U) ICON_PIC_LOSE;
	                 else
					  	hwnd->draw_info[preview_focus_Pic+1].draw_data = (INT32U) ICON_FOLDER_LOSE;	
				  	}

				  if(file_item[preview_focus_id].path_type & NAV_IS_DIR)
					  	hwnd->draw_info[preview_focus_id+2].draw_data = (INT32U) ICON_FOLDER_FOCUS;	
					  else if(file_item[preview_focus_id].path_type & MEDIA_PIC)
					 	 hwnd->draw_info[preview_focus_id+2].draw_data = (INT32U) ICON_PIC_FOCUS;
	                  else if((file_item[preview_focus_id].filelist_item_id==0xffff))
					  	hwnd->draw_info[preview_focus_id+2].draw_data = (INT32U) ICON_PIC_DISABLE;
					  
				  AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, preview_focus_Pic+1) ;
				  AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, preview_focus_id+2) ;
				  AWPostMessage(hwnd, WM_PAINT_FINISH, 0) ;
			    }
			break;
		case UOP_NEXT_CHAP:
		case UOP_FAST_FORWARD:
		case UOP_PAGE_DOWN:
			preview_focus_Pic=preview_focus_id;
			preview_focus_id=PageUp_Down(hwnd, 1, MEDIA_PIC & NAV_PATH_TYPE, SHOW_WINDOW_NUM);
			if(preview_focus_id){
				char *dev_str = FileNavGetDeviceTypeStr(FileNavGetCurrDir());
				if(dev_str){
					//if(Is_Gallery(dev_str))  {
              				//re_calculate_preview_files_num(0); /* recalculate preview_files_pages,etc */
					//}
				}
				for(INT8U i=0;i<SHOW_WINDOW_NUM;i++)
					AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, i+2) ;
				AWPostMessage(hwnd, WM_PAINT_FINISH, 0) ;
				preview_current_page++;
				RefreshCurFileName();
			}	   
			else
				{
				   if(preview_focus_Pic!=0)
				  	{
				     if(file_item[preview_focus_Pic-1].path_type & MEDIA_PIC)
					 	 hwnd->draw_info[preview_focus_Pic+1].draw_data = (INT32U) ICON_PIC_LOSE;
	                 else
					  	hwnd->draw_info[preview_focus_Pic+1].draw_data = (INT32U) ICON_FOLDER_LOSE;	
				  	}

				  if(file_item[preview_focus_id].path_type & NAV_IS_DIR)
					  	hwnd->draw_info[preview_focus_id+2].draw_data = (INT32U) ICON_FOLDER_FOCUS;	
					  else if(file_item[preview_focus_id].path_type & MEDIA_PIC)
					 	 hwnd->draw_info[preview_focus_id+2].draw_data = (INT32U) ICON_PIC_FOCUS;
	                  else if((file_item[preview_focus_id].filelist_item_id==0xffff))
					  	hwnd->draw_info[preview_focus_id+2].draw_data = (INT32U) ICON_PIC_DISABLE;
					  
				  AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, preview_focus_Pic+1) ;
				  AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, preview_focus_id+2) ;
				  AWPostMessage(hwnd, WM_PAINT_FINISH, 0) ;
			    }
			break;			
	}
}

void Preview_browser_task_post(void)
{    
    Win15PicBar_netOnKeyDo(bk_key);    
    keydown_flag=0;
}    

static void win_switch_timer_func(void *arg)
{
	if(!CheckMediaPlaybackVideoApp())
       {
		if (win_switch_timer){
	       	AVTimerDelete(win_switch_timer);
	       	win_switch_timer= 0;
	    	}
              AWGUISendNMMessage(MEDIA_GUI,NM_PREVIEW_READY,0);
	}
}

INT32S Win15PicBar_netOnKeyPress(PHWND hwnd, INT32S lpParam)
{
 	INT32U *key_value = (INT32U *)lpParam ;
	INT8U temp_index;
	PreviewFileListItem *cur_file_item=NULL;
	INT32U key = 0;

	if(keydown_flag){
	 	*key_value = UOP_NOP;
	 	return 1;
    	}	
	bk_key=*key_value;
	if(preview_current_page<preview_files_pages)
		current_frame_number=SHOW_WINDOW_NUM;
	else
		current_frame_number=preview_files_number%SHOW_WINDOW_NUM;
	if(!current_frame_number)
		current_frame_number=SHOW_WINDOW_NUM;
	if(!preview_files_number){
		current_frame_number=0;
		preview_focus_id=1;
	}
	switch (*key_value ){
	    	case UOP_RESUME:
		case UOP_OSD:
	    	case UOP_STOP:
		case UOP_ENTER:	
		case UOP_END_PAUSE:
		case UOP_NEXT_CHAP:
		case UOP_FAST_FORWARD:
	    	case UOP_PREVIOUS_CHAP:	    	
    	    	case UOP_FAST_BACKWARD:
			if(CheckMediaPlaybackVideoApp()){
				if (!win_switch_timer){
	       			keydown_flag=1;
					CloseMediaPlaybackApp(0xfe);
					win_switch_timer = AVTimerCreateFlag(100, win_switch_timer_func , &bk_key, AVTIMER_FLAG_PERIODIC);
	    			}
			}else{
				Win15PicBar_netOnKeyDo(*key_value);
    			}
			break;
		case UOP_LEFT:  
			temp_index=preview_focus_id;
			if(preview_focus_id>1){
		     		hwnd->draw_info[preview_focus_id+1].draw_data = (INT32U)GetPreviewIcon(GetFileItemType(preview_focus_id, SHOW_WINDOW_NUM), IsPreviewEnable(preview_focus_id, SHOW_WINDOW_NUM), 0);
		     		preview_focus_id--;
		     		hwnd->draw_info[preview_focus_id+1].draw_data = (INT32U)GetPreviewIcon(GetFileItemType(preview_focus_id, SHOW_WINDOW_NUM), IsPreviewEnable(preview_focus_id, SHOW_WINDOW_NUM), 1);
		     		AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, temp_index+1) ;
				AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, preview_focus_id+1) ;
              		AWPostMessage(hwnd, WM_PAINT_FINISH, 0) ;
				RefreshCurFileName();
		    	}else if((preview_focus_id==1)&&(preview_current_page>1)){
		    		bk_key = UOP_PAGE_UP;
		    		if(CheckMediaPlaybackVideoApp()){
					if (!win_switch_timer){                      
	       				keydown_flag=1;
						CloseMediaPlaybackApp(0xfe);
						win_switch_timer = AVTimerCreateFlag(100, win_switch_timer_func , &bk_key, AVTIMER_FLAG_PERIODIC);
	    				}
				}else{
					Win15PicBar_netOnKeyDo(bk_key);
    				}
		    	}
	       	break;
		 case UOP_RIGHT:
		 	temp_index=preview_focus_id;
		    	if(preview_focus_id<current_frame_number){
		     		hwnd->draw_info[preview_focus_id+1].draw_data = (INT32U)GetPreviewIcon(GetFileItemType(preview_focus_id, SHOW_WINDOW_NUM), IsPreviewEnable(preview_focus_id, SHOW_WINDOW_NUM), 0);
		     		preview_focus_id++;
		     		hwnd->draw_info[preview_focus_id+1].draw_data = (INT32U)GetPreviewIcon(GetFileItemType(preview_focus_id, SHOW_WINDOW_NUM), IsPreviewEnable(preview_focus_id, SHOW_WINDOW_NUM), 1);
		     		AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, temp_index+1) ;
				AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, preview_focus_id+1) ;
              		AWPostMessage(hwnd, WM_PAINT_FINISH, 0) ;
				RefreshCurFileName();
		    	}else if((preview_focus_id==current_frame_number)&&(preview_current_page<preview_files_pages)){
		    		bk_key = UOP_PAGE_DOWN;
		    		if(CheckMediaPlaybackVideoApp()){
					if (!win_switch_timer){
	       				keydown_flag=1;
						CloseMediaPlaybackApp(0xfe);
						win_switch_timer = AVTimerCreateFlag(100, win_switch_timer_func , &bk_key, AVTIMER_FLAG_PERIODIC);
	    				}
				}else{
					Win15PicBar_netOnKeyDo(bk_key);
    				}
		    	}
	       	break;
		case UOP_UP:
			temp_index=preview_focus_id;
			if(preview_focus_id>PREVIEW_NUM_PER_LINE){
				hwnd->draw_info[preview_focus_id+1].draw_data = (INT32U)GetPreviewIcon(GetFileItemType(preview_focus_id, SHOW_WINDOW_NUM), IsPreviewEnable(preview_focus_id, SHOW_WINDOW_NUM), 0);
		     		preview_focus_id-=PREVIEW_NUM_PER_LINE;
		     		hwnd->draw_info[preview_focus_id+1].draw_data = (INT32U)GetPreviewIcon(GetFileItemType(preview_focus_id, SHOW_WINDOW_NUM), IsPreviewEnable(preview_focus_id, SHOW_WINDOW_NUM), 1);
		     		AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, temp_index+1) ;
				AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, preview_focus_id+1) ;
              		AWPostMessage(hwnd, WM_PAINT_FINISH, 0) ;
				RefreshCurFileName();
			}else if(preview_current_page>1){
				bk_key = UOP_PAGE_UP;
		    		if(CheckMediaPlaybackVideoApp()){
					if (!win_switch_timer){                      
	       				keydown_flag=1;
						CloseMediaPlaybackApp(0xfe);
						win_switch_timer = AVTimerCreateFlag(100, win_switch_timer_func , &bk_key, AVTIMER_FLAG_PERIODIC);
	    				}
				}else{
					Win15PicBar_netOnKeyDo(bk_key);
    				}
		    	}
			break;
		case UOP_DOWN:
			temp_index=preview_focus_id;
			INT8U temp_all_line=0,temp_line=0;
			temp_all_line=(current_frame_number+(PREVIEW_NUM_PER_LINE-1))/PREVIEW_NUM_PER_LINE;
			temp_line=(preview_focus_id+(PREVIEW_NUM_PER_LINE-1))/PREVIEW_NUM_PER_LINE;
			if(temp_line<temp_all_line){
				hwnd->draw_info[preview_focus_id+1].draw_data = (INT32U)GetPreviewIcon(GetFileItemType(preview_focus_id, SHOW_WINDOW_NUM), IsPreviewEnable(preview_focus_id, SHOW_WINDOW_NUM), 0);
		     		preview_focus_id+=PREVIEW_NUM_PER_LINE;
				if(preview_focus_id>current_frame_number)
					preview_focus_id=current_frame_number;
		     		hwnd->draw_info[preview_focus_id+1].draw_data = (INT32U)GetPreviewIcon(GetFileItemType(preview_focus_id, SHOW_WINDOW_NUM), IsPreviewEnable(preview_focus_id, SHOW_WINDOW_NUM), 1);
		     		AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, temp_index+1) ;
				AWPostMessage(hwnd, WM_PAINT_DRAW_INFO, preview_focus_id+1) ;
              		AWPostMessage(hwnd, WM_PAINT_FINISH, 0) ;
				RefreshCurFileName();
			}else if(preview_current_page<preview_files_pages){
	    		    bk_key = UOP_PAGE_DOWN;
	    		    if(CheckMediaPlaybackVideoApp()){
				if (!win_switch_timer){
	       			    keydown_flag=1;
				   CloseMediaPlaybackApp(0xfe);
				    win_switch_timer = AVTimerCreateFlag(100, win_switch_timer_func , &bk_key, AVTIMER_FLAG_PERIODIC);
	    			}
			    }else{
				Win15PicBar_netOnKeyDo(bk_key);
    			    }
		    	}
			break;	
           }
	    *key_value = UOP_NOP;
    	    return 1 ;
}

INT32S Win15PicBar_netOnShow(PHWND hwnd, INT32S lpParam)
{
	INT32U temp=0;
	INT8U get_num=SHOW_WINDOW_NUM;
	INT32S start_pos = 0;
	int ret = 0;
	char *dev_str = NULL;
	if(preview_InitFileList_flag){
		Init_FileList(hwnd,SHOW_WINDOW_NUM);
              preview_select_dir_level=0;   
		dev_str = FileNavGetDeviceTypeStr(FileNavGetCurrDir());
		preview_InitFileList_flag=0;
		if(dev_str){
			INT32U file_num,dir_num;
#if 0
			if(Is_Gallery(dev_str))  {
              		//re_calculate_preview_files_num(1); /* recalculate preview_files_pages,etc */
                PATHNODE file_list;
                INT32U get_list_num = 1;
    	        FileNavSeek(0) ;
    	        dir_num=0; file_num=0;
    			while(get_list_num){
    			    get_list_num=1;
                    FileNavGetNextFileList(&file_list, &get_list_num, MEDIA_PIC ) ;
                    if(get_list_num){
                        if(file_list.path_type & NAV_IS_DIR){
                            dir_num++;
                        }
                        else{
                            file_num++;
                        }
                   }
                }         
			
		}else
#endif
		    {
				GetDirFileNum(NULL,&file_num,&dir_num,MEDIA_PIC);
            }
			preview_files_number = file_num + dir_num;
			preview_files_pages=0;
			preview_focus_id = 0;
			if(preview_files_number){
			 	preview_files_pages = (preview_files_number+(SHOW_WINDOW_NUM-1))/SHOW_WINDOW_NUM;
				preview_focus_id = 1;
			}
			preview_current_page=1;
    	}
	}else{
            	ClearPreviewflag();
		FlashPreviewFrame(hwnd,preview_focus_id,SHOW_WINDOW_NUM);
		PreviewPicPlayer(get_num);
	}
	RefreshCurFileName();
    	return 0 ;
}

INT32S Win15Pic_netOnShow(PHWND hwnd, INT32S lpParam)
{
	//unsigned char osdsettpval(unsigned char,unsigned char level);//from 0 -- 15
    osdsettpval(5,0xf);
	return 0;
}
#endif
